
public class Factory {

	private final String name;
	private List robots;

	public Factory(String name) {
		this.name = name;
		robots = new List();
	}

	public void addRobot(Robot robot) {
		this.robots.put(robot.getID(), robot);
	}

	private Robot getRobot(int id) {
		return (Robot) robots.get(id);
	}

	public void removeRobot(int id) {
		this.robots.remove(id);
	}

	public void setRole(int id, Role role) {
		this.getRobot(id).setRole(role);
	}

	public void riseHours(int id, double hours) {
		this.getRobot(id).riseHours(hours);
	}

	public void riseSpec(int id, double spec) {
		this.getRobot(id).riseSpec(spec);
	}

	public double getAvarageHours() {
		if (robots.isEmpty()) {
			return 0;
		}
		Iterator iter = robots.iterator();
		Integer key;
		Double sum = 0.0;
		while (iter.hasNext()) {
			key = (Integer) iter.next();
			sum += this.getRobot(key).getHours();
		}
		return sum / robots.getSize();
	}

	public double getAvarageRoleHours(String role) {
		if (robots.isEmpty()) {
			return 0;
		}
		Iterator iter = robots.iterator();
		Integer key;
		Double sum = 0.0;
		int count = 0;
		while (iter.hasNext()) {
			key = (Integer) iter.next();
			if (role.equals("Welder")) {
				if (this.getRobot(key).getRole() instanceof Welder) {
					sum += this.getRobot(key).getHours();
					count++;
				}
			} else if (role.equals("Painter")) {
				if (this.getRobot(key).getRole() instanceof Painter) {
					sum += this.getRobot(key).getHours();
					count++;
				}
			}
		}
		if (count == 0) {
			return 0;
		}
		return sum / count;
	}

	public double getAvarageTypeHours(String type) {
		if (robots.isEmpty()) {
			return 0;
		}
		Iterator iter = robots.iterator();
		Integer key;
		Double sum = 0.0;
		int count = 0;
		while (iter.hasNext()) {
			key = (Integer) iter.next();
			if (type.equals("Swivel")) {
				if (robots.get(key) instanceof SwivelRobot) {
					sum += this.getRobot(key).getHours();
					count++;
				}
			} else if (type.equals("Crawler")) {
				if (robots.get(key) instanceof CrawlerRobot) {
					sum += this.getRobot(key).getHours();
					count++;
				}
			}
		}
		if (count == 0) {
			return 0;
		}
		return sum / count;
	}

	public double getAvarageRotation() {
		return getAvarageSpec("Swivel");
	}

	public double getAvarageWelderRotation() {
		return getAvarageSpec("Swivel", "Welder");
	}

	public double getAvaragePainterRotation() {
		return getAvarageSpec("Swivel", "Painter");
	}

	public double getAvarageTrack() {
		return getAvarageSpec("Crawler");
	}

	public double getAvarageWelderTrack() {
		return getAvarageSpec("Crawler", "Welder");
	}

	public double getAvaragePainterTrack() {
		return getAvarageSpec("Crawler", "Painter");
	}

	private double getAvarageSpec(String type) {
		if (robots.getSize() == 0) {
			return 0;
		}
		Iterator iter = robots.iterator();
		Integer key;
		Double sum = 0.0;
		int count = 0;
		while (iter.hasNext()) {
			key = (Integer) iter.next();
			if (type.equals("Swivel")) {
				if (robots.get(key) instanceof SwivelRobot) {
					sum += this.getRobot(key).getSpec();
					count++;
				}
			} else if (type.equals("Crawler")) {
				if (robots.get(key) instanceof CrawlerRobot) {
					sum += this.getRobot(key).getSpec();
					count++;
				}
			}
		}
		if (count == 0) {
			return 0;
		}
		return sum / count;
	}

	private double getAvarageSpec(String type, String role) {
		if (robots.getSize() == 0) {
			return 0;
		}
		Iterator iter = robots.iterator();
		Integer key;
		Double sum = 0.0;
		int count = 0;
		while (iter.hasNext()) {
			key = (Integer) iter.next();
			Robot robot = this.getRobot(key);
			if (type.equals("Swivel") && role.equals("Welder")) {
				if (robot instanceof SwivelRobot && robot.getRole() instanceof Welder) {
					sum += robot.getSpec();
					count++;
				}
			} else if (type.equals("Swivel") && role.equals("Painter")) {
				if (robot instanceof SwivelRobot && robot.getRole() instanceof Painter) {
					sum += robot.getSpec();
					count++;
				}
			} else if (type.equals("Crawler") && role.equals("Welder")) {
				if (robot instanceof CrawlerRobot && robot.getRole() instanceof Welder) {
					sum += robot.getSpec();
					count++;
				}
			} else if (type.equals("Crawler") && role.equals("Painter")) {
				if (robot instanceof CrawlerRobot && robot.getRole() instanceof Painter) {
					sum += robot.getSpec();
					count++;
				}
			}
		}
		if (count == 0) {
			return 0;
		}
		return sum / count;
	}

	public int getMinTemperature() {
		return getMinTemperature(null);
	}

	public int getSwivelMinTemperature() {
		return getMinTemperature("Swivel");
	}

	public int getCrawlerMinTemperature() {
		return getMinTemperature("Crawler");
	}

	public int getMaxTemperature() {
		return getMaxTemperature(null);
	}

	public int getSwivelMaxTemperature() {
		return getMaxTemperature("Swivel");
	}

	public int getCrawlerMaxTemperature() {
		return getMaxTemperature("Crawler");
	}

	private int getMinTemperature(String type) {
		if (robots.getSize() == 0) {
			return 0;
		}
		Iterator iter = robots.iterator();
		Integer key;
		double min = this.getRobot((Integer) iter.next()).getRole().getRoleInfo();
		while (iter.hasNext()) {
			key = (Integer) iter.next();
			Robot robot = this.getRobot(key);
			if (type == null) {
				if (robot.getRole() instanceof Welder) {
					if (robot.getRole().getRoleInfo() < min) {
						min = robot.getRole().getRoleInfo();
					}
				}
			} else if (type.equals("Swivel")) {
				if (robot.getRole() instanceof Welder && robot instanceof SwivelRobot) {
					if (robot.getRole().getRoleInfo() < min) {
						min = robot.getRole().getRoleInfo();
					}
				}
			} else if (type.equals("Crawler")) {
				if (robot.getRole() instanceof Welder && robot instanceof CrawlerRobot) {
					if (robot.getRole().getRoleInfo() < min) {
						min = robot.getRole().getRoleInfo();
					}
				}
			}
		}
		return (int) min;
	}

	private int getMaxTemperature(String type) {
		if (robots.getSize() == 0) {
			return 0;
		}
		Iterator iter = robots.iterator();
		Integer key;
		double max = 0;
		while (iter.hasNext()) {
			key = (Integer) iter.next();
			Robot robot = this.getRobot(key);
			if (type == null) {
				if (robot.getRole() instanceof Welder) {
					if (robot.getRole().getRoleInfo() > max) {
						max = robot.getRole().getRoleInfo();
					}
				}
			} else if (type.equals("Swivel")) {
				if (robot.getRole() instanceof Welder && robot instanceof SwivelRobot) {
					if (robot.getRole().getRoleInfo() > max) {
						max = robot.getRole().getRoleInfo();
					}
				}
			} else if (type.equals("Crawler")) {
				if (robot.getRole() instanceof Welder && robot instanceof CrawlerRobot) {
					if (robot.getRole().getRoleInfo() > max) {
						max = robot.getRole().getRoleInfo();
					}
				}
			}
		}
		return (int) max;
	}
	
}